package siteflash.controller;

import java.io.IOException;
import java.util.Collections;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ManagerCache extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) 
		throws IOException {
	Cache cache = null;
	try {
        cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
    } catch (CacheException e) {
       e.printStackTrace();
    }
	
    String path = ((HttpServletRequest) req).getRequestURI();
	String alias = path.toLowerCase().replaceAll("/cache-manager/", "");
	
	if (alias.equalsIgnoreCase("all")) {
		cache.remove("tags");
		resp.getWriter().println("remove all cache");
	} else {
		if (cache.containsKey(alias)) {
			cache.remove(alias);
			resp.getWriter().println("remove cache: " + alias);
		}
	}
}
}
